package org.jivesoftware.smack;

import java.io.IOException;
import java.io.Writer;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import org.jivesoftware.smack.packet.Packet;

class PacketWriter
{
  private Thread writerThread;
  private Thread keepAliveThread;
  private Writer writer;
  private XMPPConnection connection;
  private final BlockingQueue<Packet> queue = new ArrayBlockingQueue(500, true);
  volatile boolean done;

  protected PacketWriter(XMPPConnection paramXMPPConnection)
  {
    this.connection = paramXMPPConnection;
    init();
  }

  protected void init()
  {
    this.writer = this.connection.writer;
    this.done = false;
    this.writerThread = new Thread()
    {
      public void run()
      {
        PacketWriter.this.writePackets(this);
      }
    };
    this.writerThread.setName("Smack Packet Writer (" + this.connection.connectionCounterValue + ")");
    this.writerThread.setDaemon(true);
  }

  public void sendPacket(Packet paramPacket)
  {
    if (!this.done)
    {
      this.connection.firePacketInterceptors(paramPacket);
      try
      {
        this.queue.put(paramPacket);
      }
      catch (InterruptedException localInterruptedException)
      {
        localInterruptedException.printStackTrace();
        return;
      }
      synchronized (this.queue)
      {
        this.queue.notifyAll();
      }
      this.connection.firePacketSendingListeners(paramPacket);
    }
  }

  public void startup()
  {
    this.writerThread.start();
  }

  void setWriter(Writer paramWriter)
  {
    this.writer = paramWriter;
  }

  public void shutdown()
  {
    this.done = true;
    synchronized (this.queue)
    {
      this.queue.notifyAll();
    }
    if (this.keepAliveThread != null)
      this.keepAliveThread.interrupt();
  }

  private Packet nextPacket()
  {
    Packet localPacket = null;
    while ((!this.done) && ((localPacket = (Packet)this.queue.poll()) == null))
      try
      {
        synchronized (this.queue)
        {
          this.queue.wait();
        }
      }
      catch (InterruptedException localInterruptedException)
      {
      }
    return localPacket;
  }

  private void writePackets(Thread paramThread)
  {
    try
    {
      openStream();
      Packet localPacket;
      while ((!this.done) && (this.writerThread == paramThread))
      {
        localPacket = nextPacket();
        if (localPacket != null)
        {
          this.writer.write(localPacket.toXML());
          if (this.queue.isEmpty())
            this.writer.flush();
        }
      }
      try
      {
        while (!this.queue.isEmpty())
        {
          localPacket = (Packet)this.queue.remove();
          this.writer.write(localPacket.toXML());
        }
        this.writer.flush();
      }
      catch (Exception localException1)
      {
        localException1.printStackTrace();
      }
      this.queue.clear();
      try
      {
        this.writer.write("</stream:stream>");
        this.writer.flush();
      }
      catch (Exception localException2)
      {
        try
        {
          this.writer.close();
        }
        catch (Exception localException3)
        {
        }
      }
      finally
      {
        try
        {
          this.writer.close();
        }
        catch (Exception localException4)
        {
        }
      }
      try
      {
        this.writer.close();
      }
      catch (Exception localException5)
      {
      }
    }
    catch (IOException localIOException)
    {
      if ((!this.done) && (!this.connection.isSocketClosed()))
      {
        this.done = true;
        if (this.connection.packetReader != null)
          this.connection.notifyConnectionError(localIOException);
      }
    }
  }

  void openStream()
    throws IOException
  {
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("<stream:stream");
    localStringBuilder.append(" to=\"").append(this.connection.getServiceName()).append("\"");
    localStringBuilder.append(" xmlns=\"jabber:client\"");
    localStringBuilder.append(" xmlns:stream=\"http://etherx.jabber.org/streams\"");
    localStringBuilder.append(" version=\"1.0\">");
    this.writer.write(localStringBuilder.toString());
    this.writer.flush();
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smack.PacketWriter
 * JD-Core Version:    0.6.1
 */